<?php

/**
 * index.php - Master Index Page Redirector
 *
 * This file is the one of the most important files and repsonsible for
 * global site security and URL rewrite handling.
 *
 * @package    BABEL
 * @author     Magnus Jonsson <magnus.jonsson@codealliance.se>
 * @author     Hannes Magnusson <hannes.magnusson@gmail.com>
 * @copyright  2008 Team babel
 * @since      Milestone 0.1
 * @license    LGPL
 */

$ROOT_DIR = getcwd() . "/../";
include($ROOT_DIR . 'includes/babel/config.php');
include($ROOT_DIR . 'includes/babel/login.php');
include($ROOT_DIR . 'includes/babel/error_handler.php');

session_start();

/**
 * PAGE ARRAYS
 */
$open_pages = array(
			'index',
			'login',
			'logout',
			'babel/index',
			'babel/ajax',
			'contact',
			'terms',
			'about',
			);

$auth_pages = array(
				'babel',
				'babel/profile',
				'babel/translate',
				'babel/ajax/translate',
				'babel/ajax/vote',
				'babel/admin/manage',
				'babel/admin/translations',
				'babel/admin/disputes',
				'babel/admin/import',
				'babel/admin/upload',
				'babel/admin/news',
				'babel/ajax/upload',
				'babel/admin/export',
				'babel/ajax/pro',
				'babel/admin/ajax/update_translations',
				'babel/api/translate',
				'babel/ajax/set_default',
				'babel/ajax/getskill',
				'babel/ajax/remove',
				'babel/ajax/add',
				);


/**
 * PAGE PROPERTIES
 *
 * Used to extended features for special pages
 */

$page_properties = array(
					'login' => array(
						'filename'		=> 'login.php',
						'skip_prepend'	=> false,
						'skip_append'	=> false,
						'args_vars'  	=> array(
						),
					),
						'contact' => array(
						'filename'		=> 'contact.html',
						'skip_prepend'	=> false,
						'skip_append'	=> false,
						'args_vars'  	=> array(
						),
					),
					'logout' => array(
						'filename'		=> 'logout.php',
						'skip_prepend'	=> true,
						'skip_append'	=> true,
						'args_vars'  	=> array(
						),
					),
					'babel' => array(
						'filename'		=> 'babel/welcome.php',
						'skip_prepend'	=> false,
						'skip_append'	=> false,
						'args_vars'  	=> array(
											2=>'action',
											),
						'additional_js' => array(
							"/js/prototype/prototype.js" => false,
							"/js/bramus/jsProgressBarHandler.js" => false,
						),
						'additional_css' => array(
							"welcome.css" => true,
						),

						),
					'babel/admin/manage' => array(
						'filename'		=> 'babel/manage_languages.php',
						'skip_prepend'	=> false,
						'skip_append'	=> false,
						'args_vars'		=> array(
											3=>'action',
											)
						),
					'babel/admin/news'	=> array(
						'filename'		=> 'babel/manage_news.php',
						'skip_prepend'	=> false,
						'skip_append'	=> false,
						'args_vars'		=> array(
											)
						),
					'babel/admin/translations' => array(
						'filename'		=> 'babel/view_translations.php',
						'skip_prepend'	=> false,
						'skip_append'	=> false,
						'args_vars'		=> array(
											)
						),
					'babel/profile' => array(
						'filename'		=> 'babel/profile.php',
						'skip_prepend'	=> false,
						'skip_append'	=> false,
						'args_vars'		=> array(
											)
						),
					'babel/ajax/set_default' 			=> array(
						'filename' 		=> 'babel/ajax/set_active_language.php',
						'skip_prepend'  => TRUE,
						'skip_append'  	=> TRUE,
						'args_vars'  	=> array(
											3=>'action',
											)
						),
					'babel/ajax/getskill' => array(
						'filename' 		=> 'babel/ajax/getSkill.php',
						'skip_prepend'  => TRUE,
						'skip_append'  	=> TRUE,
						'args_vars'  	=> array(
								3 => 'lang',
							)
						),

					'babel/ajax/remove' 			=> array(
						'filename' 		=> 'babel/ajax/remove_language.php',
						'skip_prepend'  => TRUE,
						'skip_append'  	=> TRUE,
						'args_vars'  	=> array(
											3=>'action',
											)
						),
					'babel/ajax/add' 			=> array(
						'filename' 		=> 'babel/ajax/add_language.php',
						'skip_prepend'  => TRUE,
						'skip_append'  	=> TRUE,
						'args_vars'  	=> array(
											3=>'action',
											)
						),
					'babel/translate' 	=> array(
						'filename'		=> 'babel/translate.php',
						'skip_prepend'	=> false,
						'skip_append'	=> false,
						'args_vars'  	=> array(
											2=>'action',
											)
						),
					'babel/ajax/translate' => array(
						'filename' 		=> 'babel/ajax/translate.php',
						'skip_prepend'  => true,
						'skip_append'  	=> true,
						'args_vars'  	=> array(
											)
						),
					'babel/ajax/vote' => array(
						'filename' 		=> 'babel/ajax/vote.php',
						'skip_prepend'  => true,
						'skip_append'  	=> true,
						'args_vars'  	=> array(
											)
						),
					'babel/admin/disputes' 	=> array(
						'filename'		=> 'babel/view_disputes.php',
						'skip_prepend'	=> false,
						'skip_append'	=> false,
						'args_vars'  	=> array(
											)
						),
					'babel/admin/import'	=> array(
						'filename'		=> 'babel/import.php',
						'skip_prepend'	=> false,
						'skip_append'	=> false,
						'args_vars'  	=> array(
						),
						'additional_js' => array(
							"/yui/yahoo.js"     => false,
							"/yui/event.js"     => false,
							"/yui/dom.js"       => false,
							"/yui/animation.js" => false,
							"/yui/dragdrop.js"  => false,
							"/yui/connection.js" => false,
							"/yui/container.js" => false,
						),
						'additional_css' => array(
							"/yui/build/container/assets/container.css" => false,
						),
					),
					'babel/admin/upload'	=> array(
						'filename'		=> 'babel/upload.php',
						'skip_prepend'	=> true,
						'skip_append'	=> true,
						'args_vars'  	=> array(
							3 => "filename",
						),
					),
					'babel/ajax/upload'	=> array(
						'filename' 		=> 'babel/ajax/upload.php',
						'skip_prepend'  => true,
						'skip_append'  	=> true,
						'args_vars'  	=> array(
											3=>'progress_key',
										)
						),
					'babel/admin/export' => array(
						'filename'		=> 'babel/export.php',
						'skip_prepend'	=> true,
						'skip_append'	=> false,
						'args_vars'		=> array(
												 4 => "language",
												 5 => "action",
											)
						),
					'babel/ajax/pro' 			=> array(
						'filename' 		=> 'babel/ajax/protected/ajax.php',
						'skip_prepend'  => TRUE,
						'skip_append'  	=> TRUE,
						'args_vars'  	=> array(
											3=>'page',
											4=>'function',
											5=>'action')
						),
					'babel/admin/ajax/update_translations' 	=> array(
						'filename' 		=> 'babel/ajax/update_translations.php',
						'skip_prepend'  => TRUE,
						'skip_append'  	=> TRUE,
						'args_vars'  	=> array(
											3=>'page',
											4=>'function',
											5=>'action')
						),
					'babel/api/translate' 	=> array(
						'filename'		=> 'babel/api/translate.php',
						'skip_prepend'	=> true,
						'skip_append'	=> true,
						'args_vars'  	=> array(
											3=>'langid',
											4=>'constid',
											5=>'serialize',
											)
						),
						);

$p = isset($_GET['p']) ? $_GET['p'] : "";
if ($p) {
	if (!strncmp($p,'go/',3)) {
		$p = substr($p, 3);
	}
} else {
	if( !$_SESSION['user']['username'] ) {
		$p = 'index';
	} else {
		$p = 'babel';
	}
}

/**
 * SPECIAL TREATMENT OF PATH HIDDEN VARIABLES IN URL
 */
$url_parts = explode("/",$p);
if (in_array($url_parts[0],$open_pages)) {
	if (is_array($page_properties[$url_parts[0]])) {
		$current_props = $page_properties[$url_parts[0]];
		$p = $url_parts[0];
		foreach($current_props['args_vars'] as $key=>$value) {
			if ($url_parts[$key] && $value) {
				$_GET[$value] = $url_parts[$key];
			}
		}
	}
}
if (isset($url_parts[2])) {
	$url_parts_multi = ($url_parts[0].'/'.$url_parts[1].'/'.$url_parts[2]);
} elseif (isset($url_parts[1])) {
	$url_parts_multi = ($url_parts[0].'/'.$url_parts[1]);
}else{
	$url_parts_multi = ($url_parts[0].'/');
}

if (in_array($url_parts_multi,$auth_pages)) {
	if (is_array($page_properties[$url_parts_multi]['args_vars'])) {
		$current_props = $page_properties[$url_parts_multi]['args_vars'];
		$p = $url_parts_multi;
		foreach($current_props as $key=>$value) {
			if (isset($url_parts[$key]) && $value) {
				$_GET[$value] = $url_parts[$key];
			}
		}
	}
}

// Other pages may be using this somewhere
$_GET['p'] = $p;

/**
 * Load language file
 */
if ((in_array($p,$open_pages) || in_array($p,$auth_pages)) && isset($_SESSION['params'])) {
	$language_file = _ICE_DIR.'languages/'.$p.'.lang.'.$_SESSION['params']['lang'];

	if(file_exists($language_file))
		include_once($language_file);
}

/**
 * Open Page
 *
 * Check if page exists in array, and if so, include the correct prepend files depending
 * on the authentication
 *
 */
$current_props = $page_properties[$p];
if (isset($current_props['additional_js'])) {
	$ADDITIONAL_JS = $current_props['additional_js'];
}
if (isset($current_props['additional_css'])) {
	$ADDITIONAL_CSS = $current_props['additional_css'];
}


if (in_array($p,$open_pages)) {

	if (!$current_props['skip_prepend']) {
		if( !isset($_SESSION['user']['username']) ) {
			include($ROOT_DIR . 'includes/babel/prepend_no_gui_openweb.php');
		} else {
			include($ROOT_DIR . 'includes/babel/prepend_no_gui.php');
		}

		/**
		 * Special Handler for Username in the URL ex: expono.com/magnus
		 */
		if (isset($_GET['u'])) {
			$u = $_GET['u'];
			$user = array();
			$user['user_id'] = $id = $obj_users->getID($u);
			if ($user['profile'] = $obj_users->getProfile($id)) {
				$user['user_data'] = $obj_users->getUserData($id);
				$user['username'] = $u;
			} else {
				if ($redirect_to = $obj_users->getRedirectUser($u)) {
					header("Location: /$redirect_to");
					exit;
				} else {
					header("Location: /go/user_not_found");
					exit;
				}
			}
		}

		if( !isset($_SESSION['user']['username'])) {
			include($ROOT_DIR . 'includes/babel/prepend_openweb.php');
		} else {
			include($ROOT_DIR . 'includes/babel/prepend.php');
		}
	}

	if ($current_props['filename']) {
		$filename = _ICE_DIR.'files/'.$current_props['filename'];
	} else {
		$filename = _ICE_DIR.'files/'.$p.'.php';
	}

	include($filename);
	if (!$current_props['skip_append']) {
		if( !isset($_SESSION['user']['username']) && !_ICE_DEBUG) {
			include($ROOT_DIR . 'includes/babel/append_openweb.php');
		} else {
			include($ROOT_DIR . 'includes/babel/append.php');
		}
	}

} elseif (in_array($p,$auth_pages)) {
	if (!$current_props['skip_prepend'] && (!isset($current_props['skip_auth']) || !$current_props['skip_auth'])) {
		include($ROOT_DIR . 'includes/babel/prepend.php');
	}
	if ($current_props['filename']) {
		$filename = _ICE_DIR.'files/'.$current_props['filename'];
	} else {
		$filename = _ICE_DIR.'files/'.$p.'.php';
	}

	include($filename);

	if (!$current_props['skip_append'] && (!isset($current_props['skip_auth']) || !$current_props['skip_auth'])) {
		include($ROOT_DIR . 'includes/babel/append.php');
	}

} else {
	if( !$_SESSION['user']['username'] ) {
		if (_ICE_DEBUG) {
			header("Location: /go/login");
			exit;
		}
		include($ROOT_DIR . 'includes/babel/prepend_openweb.php');
	} else {
		include($ROOT_DIR . 'includes/babel/prepend.php');
	}

	include(_ICE_DIR.'files/error.php');

	if( !$_SESSION['user']['username'] ) {
		include($ROOT_DIR . 'includes/babel/append_openweb.php');
	} else {
		include($ROOT_DIR . 'includes/babel/append.php');
	}
}


